﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

namespace WeakEndForum
{
    public partial class NewArticle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Modules.isUserConnected())
            {
                Response.Redirect("Error401.aspx");
            }
            else if (!Modules.isMod(Modules.getConnectedUser()))
            {
                Response.Redirect("Error403.aspx");
            }
        }

        protected void btnSendNewArticle_Click(object sender, EventArgs e)
        {
            string articleMessage = txtNewArticleContent.Text;
            string articleTitle = txtNewArticleTitle.Text;
            string articleAuthor = Modules.getConnectedUser();

            OleDbConnection conn = new OleDbConnection(
                ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
            conn.Open();
            OleDbCommand newArticleCommand = new OleDbCommand("INSERT INTO Actualite (Titre, Contenu, Auteur) VALUES (@paramTitre, @paramContenu, @paramAuteur);", conn);
            newArticleCommand.Parameters.Add(new OleDbParameter("paramTitre", articleTitle) { OleDbType = OleDbType.VarChar, Size = 255 });
            newArticleCommand.Parameters.Add(new OleDbParameter("paramContent", articleMessage) { OleDbType = OleDbType.LongVarChar, Size = 65535 });
            newArticleCommand.Parameters.Add(new OleDbParameter("paramAuteur", articleAuthor) { OleDbType = OleDbType.LongVarChar, Size = 65535 });
            newArticleCommand.Prepare();
            OleDbDataReader dataReader = newArticleCommand.ExecuteReader();
            OleDbCommand getNewArticleID = new OleDbCommand("SELECT @@IDENTITY;", conn);
            int newPostID = (int)getNewArticleID.ExecuteScalar();
            conn.Close();
            Response.Redirect("Article.aspx?aID=" + newPostID.ToString());

        }
    }
}